home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #23 (Aug 87) / color menus source / MenuFun Source < prev    next >
Text File  |  1987-06-17  |  10KB  |  422 lines

  1. {        MenuFun by Steve Sheets 6/3/87        }
  2.  
  3. {    Simple Demonstration of Mac // Menu Manager.        }
  4.  
  5. PROGRAM LMC;
  6.  
  7. {    Various Constants:    Menu ID Numbers  }
  8.     CONST
  9.         AppleMenuID = 40;
  10.         FileMenuID = 41;
  11.         EditMenuID = 42;
  12.         DisplayMenuID = 43;
  13.         ColorMenuID = 44;
  14.         FontMenuID = 45;
  15.         SizeMenuID = 46;
  16.         StyleMenuID = 47;
  17.         JustMenuID = 48;
  18.         FColorMenuID = 49;
  19.         BColorMenuID = 50;
  20.  
  21. {    Various Variables:    Menus, Window, Text, Rectangle, Done Flag,    }
  22. {        Colors, Font, Size and Justification Settings.    }
  23.     VAR
  24.         AppleMenu, FileMenu, EditMenu, DisplayMenu, ColorMenu, FontMenu, SizeMenu, StyleMenu, JustMenu, FColorMenu, BColorMenu : MenuHandle;
  25.         MyWindow : windowptr;
  26.         Done : boolean;
  27.         MyStr : str255;
  28.         MyRect : Rect;
  29.         MyFColor, MyBColor, MyFont, MySize, MyJust : integer;
  30.         MyBold, MyItalic, MyUnderline, MyOutline, MyShadow, MyCondense, MyExtend : boolean;
  31.         MyStyle : Style;
  32.  
  33. {    Redraws the window, by creating an update event.    }
  34.     PROCEDURE ReDraw;
  35.         VAR
  36.             tempPort : Grafptr;
  37.     BEGIN
  38.         GetPort(tempPort);
  39.         SetPort(MyWindow);
  40.         InvalRect(MyRect);
  41.         SetPort(tempPort);
  42.     END;
  43.  
  44. {    Given the style settings, checks the various Style Menus and creates    }
  45. {        the correct Style variable.    }
  46.     PROCEDURE CheckStyle;
  47.     BEGIN
  48.         MyStyle := [];
  49.         IF MyBold THEN
  50.             BEGIN
  51.                 SetItemMark(StyleMenu, 1, CHAR(diamondMark));
  52.                 MyStyle := MyStyle + [Bold];
  53.             END
  54.         ELSE
  55.             SetItemMark(StyleMenu, 1, CHAR(noMark));
  56.         IF MyItalic THEN
  57.             BEGIN
  58.                 SetItemMark(StyleMenu, 2, CHAR(diamondMark));
  59.                 MyStyle := MyStyle + [Italic];
  60.             END
  61.         ELSE
  62.             SetItemMark(StyleMenu, 2, CHAR(noMark));
  63.         IF MyUnderLine THEN
  64.             BEGIN
  65.                 SetItemMark(StyleMenu, 3, CHAR(diamondMark));
  66.                 MyStyle := MyStyle + [Underline];
  67.             END
  68.         ELSE
  69.             SetItemMark(StyleMenu, 3, CHAR(noMark));
  70.         IF MyOutline THEN
  71.             BEGIN
  72.                 SetItemMark(StyleMenu, 4, CHAR(diamondMark));
  73.                 MyStyle := MyStyle + [Outline];
  74.             END
  75.         ELSE
  76.             SetItemMark(StyleMenu, 4, CHAR(noMark));
  77.         IF MyShadow THEN
  78.             BEGIN
  79.                 SetItemMark(StyleMenu, 5, CHAR(diamondMark));
  80.                 MyStyle := MyStyle + [Shadow];
  81.             END
  82.         ELSE
  83.             SetItemMark(StyleMenu, 5, CHAR(noMark));
  84.         IF MyCondense THEN
  85.             BEGIN
  86.                 SetItemMark(StyleMenu, 6, CHAR(diamondMark));
  87.                 MyStyle := MyStyle + [Condense];
  88.             END
  89.         ELSE
  90.             SetItemMark(StyleMenu, 6, CHAR(noMark));
  91.         IF MyExtend THEN
  92.             BEGIN
  93.                 SetItemMark(StyleMenu, 7, CHAR(diamondMark));
  94.                 MyStyle := MyStyle + [Extend];
  95.             END
  96.         ELSE
  97.             SetItemMark(StyleMenu, 7, CHAR(noMark));
  98.     END;
  99.  
  100. {    Given the color number (1-8), returns the correct color constant.    }
  101.     FUNCTION GetColor (C : integer) : longint;
  102.     BEGIN
  103.         CASE C OF
  104.             1 : 
  105.                 GetColor := BlackColor;
  106.             2 : 
  107.                 GetColor := WhiteColor;
  108.             3 : 
  109.                 GetColor := RedColor;
  110.             4 : 
  111.                 GetColor := GreenColor;
  112.             5 : 
  113.                 GetColor := BlueColor;
  114.             6 : 
  115.                 GetColor := CyanColor;
  116.             7 : 
  117.                 GetColor := MagentaColor;
  118.             8 : 
  119.                 GetColor := YellowColor;
  120.         END;
  121.     END;
  122.  
  123. {    Draws the Text in the Rectangle in the correct Colors, Font,        }
  124. {        Size, Style and Justification.    }
  125.     PROCEDURE DoDraw;
  126.         VAR
  127.             tempStr : str255;
  128.             tempInteger : integer;
  129.     BEGIN
  130.         ForeColor(GetColor(MyFColor));
  131.         BackColor(GetColor(MyBColor));
  132.         GetItem(FontMenu, MyFont, tempStr);
  133.         GetFNum(tempStr, tempInteger);
  134.         TextFont(tempInteger);
  135.         CASE MySize OF
  136.             1 : 
  137.                 TextSize(9);
  138.             2 : 
  139.                 TextSize(10);
  140.             3 : 
  141.                 TextSize(12);
  142.             4 : 
  143.                 TextSize(18);
  144.             5 : 
  145.                 TextSize(24);
  146.             6 : 
  147.                 TextSize(32);
  148.         END;
  149.         TextFace(MyStyle);
  150.         TextBox(POINTER(ord4(@MyStr) + 1), LENGTH(MyStr), MyRect, MyJust - 2);
  151.     END;
  152.  
  153. {    Edit the Text.    }
  154.     PROCEDURE DoEdit;
  155.         VAR
  156.             MyDialog : DialogPtr;
  157.             N : integer;
  158.             MyH : handle;
  159.             MyR : rect;
  160.     BEGIN
  161.         MyDialog := GetNewDialog(130, NIL, POINTER(-1));
  162.         GetDItem(MyDialog, 4, N, MyH, MyR);
  163.         SetIText(MyH, MyStr);
  164.  
  165.         REPEAT
  166.             ModalDialog(NIL, N);
  167.         UNTIL (N = 1) OR (N = 2);
  168.  
  169.         IF N = 1 THEN
  170.             BEGIN
  171.                 GetIText(MyH, MyStr);
  172.                 ReDraw;
  173.             END;
  174.  
  175.         DisposDialog(MyDialog);
  176.     END;
  177.  
  178. {    Standard main menu procedure that handles menu selections.  Can show    }
  179. {    About Box, Edit the Text, change the Done Flag (so the program quits),     }
  180. {    handle edit commands (Cut,Copy,Paste,Clear), and change all the Colors,    }
  181. {    Font, Size, Style and Justification of the Text.     }
  182.     PROCEDURE MainMenu (tempResult : LONGINT);
  183.         VAR
  184.             tempInteger : integer;
  185.             tempStr : str255;
  186.     BEGIN
  187.         tempInteger := LoWord(tempResult);
  188.         CASE HiWord(tempResult) OF
  189.             AppleMenuID : 
  190.                 IF tempInteger = 1 THEN
  191.                     tempInteger := Alert(128, NIL)
  192.                 ELSE
  193.                     BEGIN
  194.                         GetItem(appleMenu, tempInteger, tempStr);
  195.                         tempInteger := OpenDeskAcc(tempStr);
  196.                     END;
  197.             FileMenuID : 
  198.                 IF tempInteger = 1 THEN
  199.                     DoEdit
  200.                 ELSE IF tempInteger = 3 THEN
  201.                     Done := (Alert(129, NIL) = 2);
  202.             EditMenuID : 
  203.                 IF NOT SystemEdit(tempInteger - 1) THEN
  204.                     sysbeep(1);
  205.             FColorMenuID : 
  206.                 IF (tempInteger <> 0) AND (tempInteger <> MyFColor) THEN
  207.                     BEGIN
  208.                         CheckItem(FColorMenu, MyFColor, false);
  209.                         MyFColor := tempInteger;
  210.                         CheckItem(FColorMenu, MyFColor, true);
  211.                         ReDraw;
  212.                     END;
  213.             BColorMenuID : 
  214.                 IF (tempInteger <> 0) AND (tempInteger <> MyBColor) THEN
  215.                     BEGIN
  216.                         CheckItem(BColorMenu, MyBColor, false);
  217.                         MyBColor := tempInteger;
  218.                         CheckItem(BColorMenu, MyBColor, true);
  219.                         ReDraw;
  220.                     END;
  221.             FontMenuID : 
  222.                 IF (tempInteger <> 0) AND (tempInteger <> MyFont) THEN
  223.                     BEGIN
  224.                         CheckItem(FontMenu, MyFont, false);
  225.                         MyFont := tempInteger;
  226.                         CheckItem(FontMenu, MyFont, true);
  227.                         ReDraw;
  228.                     END;
  229.             SizeMenuID : 
  230.                 IF (tempInteger <> 0) AND (tempInteger <> MySize) THEN
  231.                     BEGIN
  232.                         CheckItem(SizeMenu, MySize, false);
  233.                         MySize := tempInteger;
  234.                         CheckItem(SizeMenu, MySize, true);
  235.                         ReDraw;
  236.                     END;
  237.             StyleMenuID : 
  238.                 BEGIN
  239.                     CASE tempInteger OF
  240.                         1 : 
  241.                             MyBold := NOT MyBold;
  242.                         2 : 
  243.                             MyItalic := NOT MyItalic;
  244.                         3 : 
  245.                             MyUnderLine := NOT MyUnderLine;
  246.                         4 : 
  247.                             MyOutline := NOT MyOutline;
  248.                         5 : 
  249.                             MyShadow := NOT MyShadow;
  250.                         6 : 
  251.                             MyCondense := NOT MyCondense;
  252.                         7 : 
  253.                             MyExtend := NOT MyExtend;
  254.                         OTHERWISE
  255.                     END;
  256.                     CheckStyle;
  257.                     ReDraw;
  258.                 END;
  259.             JustMenuID : 
  260.                 IF (tempInteger <> 0) AND (tempInteger <> MyJust) THEN
  261.                     BEGIN
  262.                         CheckItem(JustMenu, MyJust, false);
  263.                         MyJust := tempInteger;
  264.                         CheckItem(JustMenu, MyJust, true);
  265.                         ReDraw;
  266.                     END;
  267.             OTHERWISE
  268.         END;
  269.         HiliteMenu(0);
  270.     END;
  271.  
  272. {    Setup for Menus, Window, Done flag, Text, Rectangle, Colors, Font,    }
  273. {        Size, Style and Justification of the Text.}
  274.     PROCEDURE DoSetup;
  275.     BEGIN
  276.         AppleMenu := GetMenu(AppleMenuID);
  277.         AddResMenu(AppleMenu, 'DRVR');
  278.  
  279.         FileMenu := GetMenu(FileMenuID);
  280.  
  281.         EditMenu := GetMenu(EditMenuID);
  282.  
  283.         DisplayMenu := GetMenu(DisplayMenuID);
  284.  
  285.         ColorMenu := GetMenu(ColorMenuID);
  286.  
  287.         FontMenu := GetMenu(FontMenuID);
  288.         AddResMenu(FontMenu, 'FONT');
  289.         MyFont := 1;
  290.         CheckItem(FontMenu, MyFont, true);
  291.  
  292.         SizeMenu := GetMenu(SizeMenuID);
  293.         MySize := 3;
  294.         CheckItem(SizeMenu, MySize, true);
  295.  
  296.         StyleMenu := GetMenu(StyleMenuID);
  297.         MyBold := false;
  298.         MyItalic := false;
  299.         MyUnderLine := false;
  300.         MyOutline := false;
  301.         MyShadow := false;
  302.         MyCondense := false;
  303.         MyExtend := false;
  304.         CheckStyle;
  305.  
  306.         JustMenu := GetMenu(JustMenuID);
  307.         MyJust := 3;
  308.         CheckItem(JustMenu, MyJust, true);
  309.  
  310.         FColorMenu := GetMenu(FColorMenuID);
  311.         MyFColor := 1;
  312.         CheckItem(FColorMenu, MyFColor, true);
  313.  
  314.         BColorMenu := GetMenu(BColorMenuID);
  315.         MyBColor := 2;
  316.         CheckItem(BColorMenu, MyBColor, true);
  317.  
  318.         InsertMenu(AppleMenu, 0);
  319.         InsertMenu(FileMenu, 0);
  320.         InsertMenu(EditMenu, 0);
  321.         InsertMenu(DisplayMenu, 0);
  322.         InsertMenu(ColorMenu, -1);
  323.         InsertMenu(FontMenu, -1);
  324.         InsertMenu(SizeMenu, -1);
  325.         InsertMenu(StyleMenu, -1);
  326.         InsertMenu(JustMenu, -1);
  327.         InsertMenu(FColorMenu, -1);
  328.         InsertMenu(BColorMenu, -1);
  329.  
  330.         DrawMenuBar;
  331.  
  332.         MyWindow := GetNewWindow(128, NIL, POINTER(-1));
  333.  
  334.         MyRect := MyWindow^.portRect;
  335.  
  336.         GetIndString(MyStr, 128, 1);
  337.  
  338.         InitCursor;
  339.  
  340.         Done := false;
  341.     END;
  342.  
  343. {    Standard main program loop that handles all events (ie. mouse down, key    }
  344. {    downs & updates) until the Done flag is set.    }
  345.     PROCEDURE MainLoop;
  346.         VAR
  347.             tempEvent : EventRecord;
  348.             tempWindow : windowptr;
  349.             tempCode : integer;
  350.             tempPort : Grafptr;
  351.             tempRect : rect;
  352.             tempLong : longint;
  353.     BEGIN
  354.         REPEAT
  355.             SystemTask;
  356.             IF GetNextEvent(everyEvent, tempEvent) THEN
  357.                 BEGIN
  358.                     CASE tempEvent.what OF
  359.                         mouseDown : 
  360.                             BEGIN
  361.                                 tempCode := FindWindow(tempEvent.where, tempWindow);
  362.                                 CASE tempCode OF
  363.                                     inDrag, inContent : 
  364.                                         BEGIN
  365.                                             IF tempWindow <> FrontWindow THEN
  366.                                                 SelectWindow(tempWindow)
  367.                                             ELSE
  368.                                                 BEGIN
  369.                                                     IF MyWindow = tempWindow THEN
  370.                                                         BEGIN
  371.                                                             SetRect(tempRect, -25000, -25000, 25000, 25000);
  372.                                                             DragWindow(MyWindow, tempEvent.where, tempRect);
  373.                                                         END;
  374.                                                 END;
  375.                                         END;
  376.                                     inGrow : 
  377.                                         BEGIN
  378.                                             IF tempWindow <> FrontWindow THEN
  379.                                                 SelectWindow(tempWindow)
  380.                                             ELSE
  381.                                                 BEGIN
  382.                                                     IF MyWindow = tempWindow THEN
  383.                                                         BEGIN
  384.                                                             SetRect(tempRect, -25000, -25000, 25000, 25000);
  385.                                                             tempLong := GrowWindow(MyWindow, tempEvent.where, tempRect);
  386.                                                             SizeWindow(MyWindow, LoWord(tempLong), HiWord(tempLong), false);
  387.                                                             MyRect := MyWindow^.portRect;
  388.                                                             ReDraw;
  389.                                                         END;
  390.                                                 END;
  391.                                         END;
  392.                                     inMenuBar : 
  393.                                         MainMenu(MenuSelect(tempEvent.where));
  394.                                     inSysWindow : 
  395.                                         SystemClick(tempEvent, tempWindow);
  396.                                     OTHERWISE
  397.                                 END; { of tempCode case }
  398.                             END; { of mouseDown }
  399.                         keydown, autoKey : 
  400.                             IF BitAnd(tempEvent.modifiers, cmdKey) <> 0 THEN
  401.                                 MainMenu(MenuKey(CHR(tempEvent.message MOD 256)));
  402.                         updateEvt : 
  403.                             IF MyWindow = WindowPtr(tempEvent.message) THEN
  404.                                 BEGIN
  405.                                     GetPort(tempPort);
  406.                                     SetPort(MyWindow);
  407.                                     BeginUpdate(MyWindow);
  408.                                     DoDraw;
  409.                                     EndUpdate(MyWindow);
  410.                                     SetPort(tempPort);
  411.                                 END;
  412.                         OTHERWISE
  413.                     END;
  414.                 END;
  415.         UNTIL Done;
  416.     END;
  417.  
  418. {    ***PROGRAM***    }
  419. BEGIN
  420.     DoSetup;
  421.     MainLoop;
  422. END.